package com.sky.controller;

import com.sky.domain.Admin;
import com.sky.domain.Student;
import com.sky.domain.Teacher;
import com.sky.service.AdminService;
import com.sky.service.StudentService;
import com.sky.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 尹稳健~
 * @description: 控制器-管理个人信息管理页面
 * @version 1.0
 */
@Controller
@RequestMapping("/common")
public class CommonController {

    //注入业务对象
    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    //注入业务对象
    @Autowired
    @Qualifier("studentServiceImpl")
    private StudentService studentService;

    //注入业务对象
    @Autowired
    @Qualifier("teacherServiceImpl")
    private TeacherService teacherService;

    //存储预返回给页面的结果对象
    private Map<String,Object> result = new HashMap<String, Object>();

    /**
     * @description: 跳转到个人信息管理页面
     * @param: no
     * @return: java.lang.String
     */
    @RequestMapping("/goSettingView")
    public String goSettingView(){
        return "/common/settings";
    }

    /**
     * @description: 修改密码
     * @param: oldPassword
     * @param: newPassword
     * @param: request
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/editPassword")
    @ResponseBody
    public Map<String,Object> editPassword(String oldPassword, String newPassword, HttpServletRequest request){
        //判断当前登录用户的用户类型
        Integer userType = (Integer)request.getSession().getAttribute("userType");
        //管理员身份
        if (userType == 1){
            Admin admin = (Admin)request.getSession().getAttribute("userInfo");
            if (!admin.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码错误!");
                return result;
            }
            //修改密码
            admin.setPassword(newPassword);
            adminService.updatePassword(admin);
            result.put("success",true);
        }

        //学生身份
        if (userType == 2){
            Student student = (Student)request.getSession().getAttribute("userInfo");

            if (!student.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码错误!");
                return result;
            }
            student.setPassword(newPassword);
            studentService.updatePassword(student);
            result.put("success",true);
        }

        //教师身份
        if (userType == 3){
            Teacher teacher = (Teacher)request.getSession().getAttribute("userInfo");
            if (!teacher.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码错误!");
                return result;
            }
            teacher.setPassword(newPassword);
            teacherService.updatePassword(teacher);
            result.put("success",true);
        }

        return result;
    }
}
